﻿package com.tdzx.alipay;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;

import com.tdzx.R;


public class AlipayActivity extends Activity{
	private static final String TAG = "AlipayActivity" ;
	private Activity activity = AlipayActivity.this;
	
	// 这里接收支付结果，支付宝手机端同步通知
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			try {
				String strRet = (String) msg.obj;

				Log.e(TAG, strRet); // strRet范例：resultStatus={9000};memo={};result={partner="2088201564809153"&seller="2088201564809153"&out_trade_no="050917083121576"&subject="123456"&body="2010新款NIKE 耐克902第三代板鞋 耐克男女鞋 386201 白红"&total_fee="0.01"&notify_url="http://notify.java.jpxx.org/index.jsp"&success="true"&sign_type="RSA"&sign="d9pdkfy75G997NiPS1yZoYNCmtRbdOP0usZIMmKCCMVqbSG1P44ohvqMYRztrB6ErgEecIiPj9UldV5nSy9CrBVjV54rBGoT6VSUF/ufjJeCSuL510JwaRpHtRPeURS1LXnSrbwtdkDOktXubQKnIMg2W0PreT1mRXDSaeEECzc="}
				switch (msg.what) {
				case AlixId.RQF_PAY: {
					//
					alipayCenter.closeProgress();

					BaseHelper.log(TAG, strRet);

					// 处理交易结果
					try {
						// 获取交易状态码，具体状态代码请参看文档
						String tradeStatus = "resultStatus={";// 无多大意义
						int imemoStart = strRet.indexOf("resultStatus=");
						imemoStart += tradeStatus.length();
						int imemoEnd = strRet.indexOf("};memo=");
						tradeStatus = strRet.substring(imemoStart, imemoEnd);

						// 先验签通知
						ResultChecker resultChecker = new ResultChecker(strRet);
						int retVal = resultChecker.checkSign();
						// 验签失败
						if (retVal == ResultChecker.RESULT_CHECK_SIGN_FAILED) {
							BaseHelper.showDialog(
									activity,
									"提示",
									getResources().getString(
											R.string.check_sign_failed),
									android.R.drawable.ic_dialog_alert);
						} else {// 验签成功。验签成功后再判断交易状态码
							if (tradeStatus.equals("9000"))// 判断交易状态码，只有9000表示交易成功
								BaseHelper.showDialog(activity, "提示",
										"支付成功。交易状态码：" + tradeStatus,
										R.drawable.infoicon);
							else
								BaseHelper.showDialog(activity, "提示",
										"支付失败。交易状态码:" + tradeStatus,
										R.drawable.infoicon);
								
						}

					} catch (Exception e) {
						e.printStackTrace();
						BaseHelper.showDialog(activity, "提示", strRet,
								R.drawable.infoicon);
					}
				}
					break;
				}

				super.handleMessage(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	private AlipayCenter alipayCenter;
	private boolean isPrepare;
	private boolean isCheckForPartnerAndSeller;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hello_alipay);
		alipayCenter = new AlipayCenter(this);
		isPrepare = alipayCenter.payPrepareForcheckInstallAlipay();
		if (isPrepare) {
			isCheckForPartnerAndSeller = alipayCenter
					.checkInfoForPartnerAndSeller();
			if (isCheckForPartnerAndSeller) {
				String orderInfo = OrderInfoUtil.getOrderInfo(String.valueOf(System.currentTimeMillis()), "Android支付宝测试",
						"宽途汽车支付宝接入", "0.01");
				alipayCenter.payStart(orderInfo, mHandler);
			}

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	
}
